package cs;

import java.io.*;
import java.net.Socket;

public class FileSendThread2 extends Thread {
    private Socket s;
    public FileSendThread2(Socket s){
        this.s=s;
    }
    public static final  String FILEAME="D:\\我的文档\\My Pictures";
    // public static final  String FILEAME="/opt/javademo/pic";

    //客户端告诉我文件名,我把文件发过去
    @Override
    public void run() {
        System.out.println(s+"连接了");
        FileInputStream fin= null;
        OutputStream out=null;
        try {
            InputStream in=s.getInputStream();
             out=s.getOutputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(in));
            String fileName=br.readLine();
            fin = new FileInputStream(new File(FILEAME,fileName));

            byte[]b=new byte[fin.available()];
            int len=fin.read(b);
            fin.close();
            //读完内容,准备发送

            out.write(b,0,len);
            out.flush();
            System.out.println("文件发送给"+s+"成功");
        } catch (Exception e) {
            //if(e instanceof FileNotFoundException){
            try {
                out.write(("读取出现异常"+e).getBytes());
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            //}
            e.printStackTrace();
        }finally {
            if(s!=null&&!s.isClosed()) {
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
